﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.CustomerManage;
using MINERP.ComControls;
using FastReport;

namespace ERPGoodsDelivery
{
    public partial class FrmShippingFee : Form
    {
        public FrmShippingFee()
        {
            InitializeComponent();
        }
        public int UserID = 0;
        PrinterBill pbill = new PrinterBill();
        private void simpleButtonSavePrint_Click(object sender, EventArgs e)
        {
            try
            {
                simpleButtonSavePrint.Enabled = false;
                simpleButtonSave.Enabled = false;
                Cursor = Cursors.WaitCursor;
                Shop_Info info = (Shop_Info)cmbShopList.SelectedItem;
                if (info == null)
                {
                    MessageBox.Show("未选择商户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (string.IsNullOrEmpty(textEditName.Text.Trim()))
                {
                    MessageBox.Show("收费名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;

                }
                if (string.IsNullOrEmpty(textEditUnitPrice.Text.Trim()))
                {
                    MessageBox.Show("单价不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;

                }
                if (string.IsNullOrEmpty(textEditNum.Text.Trim()))
                {
                    MessageBox.Show("数量不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                SHOP_Bill_Info sbi = _bll.SetShippingFee(textEditName.Text.Trim(), info.ShopId, Convert.ToDecimal(textEditUnitPrice.Text.Trim()), Convert.ToDecimal(textEditNum.Text.Trim()), UserID);

                DataSet set = pbill.PrintBill(sbi);
                set.WriteXml(Application.StartupPath + @"\PrintShopBill.xml", XmlWriteMode.WriteSchema);
                Report report = new Report();
                report.Load(Application.StartupPath + "\\Report\\PrintShopBill.frx");
                report.RegisterData(set.Tables["tableTitle"], "tableTitle");
                report.RegisterData(set.Tables["tableDetail"], "tableDetail");
                report.RegisterData(set.Tables["tableSubDetail"], "tableSubDetail");
                //report.RegisterData(tableBottom, "tableBottom");
                //设置合计参数

                report.SetParameterValue("Para_TotalNum", set.Tables["tableTitle"].Rows[0]["Para_TotalNum"]);
                report.SetParameterValue("Para_TotalAreas", set.Tables["tableTitle"].Rows[0]["Para_TotalAreas"]);
                report.SetParameterValue("Para_TotalCount", set.Tables["tableTitle"].Rows[0]["Para_TotalCount"]);
                report.SetParameterValue("Para_Sum", set.Tables["tableTitle"].Rows[0]["Para_Sum"]);
                report.SetParameterValue("Para_TotalPrintCount", set.Tables["tableTitle"].Rows[0]["Para_TotalPrintCount"]);
                report.SetParameterValue("Para_TotalProductCount", set.Tables["tableTitle"].Rows[0]["Para_TotalProductCount"]);
                report.PrintSettings.ShowDialog = false;
                report.Print();
                MessageBox.Show("打印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception exz)
            {

            }
            finally
            {
                simpleButtonSavePrint.Enabled = true;
                simpleButtonSave.Enabled = true;
                Cursor = Cursors.Default;
            }
            
        }

        private void simpleButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                simpleButtonSavePrint.Enabled = false;
                simpleButtonSave.Enabled = false;
                Cursor = Cursors.WaitCursor;
                Shop_Info info = (Shop_Info)cmbShopList.SelectedItem;
                if (info == null)
                {
                    MessageBox.Show("未选择商户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (string.IsNullOrEmpty(textEditName.Text.Trim()))
                {
                    MessageBox.Show("收费名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;

                }
                if (string.IsNullOrEmpty(textEditUnitPrice.Text.Trim()))
                {
                    MessageBox.Show("单价不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;

                }
                if (string.IsNullOrEmpty(textEditNum.Text.Trim()))
                {
                    MessageBox.Show("数量不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                SHOP_Bill_Info sbi = _bll.SetShippingFee(textEditName.Text.Trim(), info.ShopId, Convert.ToDecimal(textEditUnitPrice.Text.Trim()), Convert.ToDecimal(textEditNum.Text.Trim()), UserID);
                MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception exxx)
            {

            }
            finally
            {
                simpleButtonSavePrint.Enabled = true;
                simpleButtonSave.Enabled = true;
                Cursor = Cursors.Default;
            }
        }
        List<Shop_Info> listShop = new List<Shop_Info>();
        PrePrintBillBLL _bll = new PrePrintBillBLL();
        private void FrmShippingFee_Load(object sender, EventArgs e)
        {
            listShop = _bll.GetShopList();
        }

        private void txtShopMnemonicCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keys.Enter == e.KeyCode)
            {
                string strHelpCode = txtShopMnemonicCode.Text.Trim();
                cmbShopList.DisplayMember = "ShopName";
                cmbShopList.ValueMember = "ShopId";
                cmbShopList.DataSource = listShop.Where(s=>s.MnemonicCode.Contains(strHelpCode)).ToList();


            }
        }

        private void txtShopMnemonicCode_EditValueChanged(object sender, EventArgs e)
        {
            string strHelpCode = txtShopMnemonicCode.Text.Trim().ToUpper();
            cmbShopList.DisplayMember = "ShopName";
            cmbShopList.ValueMember = "ShopId";
            cmbShopList.DataSource = listShop.Where(s => s.MnemonicCode.Contains(strHelpCode)).ToList();
        }
    }
}
